﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.Model.Member;
using ECommerce.BLL.Member;
using Jzy.Core.Utility;
using Jzy.Core.Common.Basic;
using System.Net.Mail;
using Jzy.Core.Log.Exceptions;
using System.Net;

namespace ECommerce.Web.Site
{
    public partial class Login : SitePage
    {
        private string FromUrl
        {
            get
            {
                return Request.QueryString["FromUrl"];
            }
            
        }

        private MemberBaseBO Member
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultButton = btnLogin.UniqueID;
        }

       

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string strLoginName = txtLoginName.Text.Trim();
            string strPassword = txtPassword.Text.Trim();


            CheckLogin(strLoginName, strPassword);
        }

        private void CheckLogin(string strLoginName, string strPassword)
        {            

            LoginResult result = Member.CheckLogin(strLoginName, strPassword);

            bool blnFlag = true;
            switch (result)
            {
                case LoginResult.Forbidden:
                case LoginResult.PwdErr:
                case LoginResult.UserErr:
                    blnFlag = false;
                    break;
            }

            if (blnFlag)
            {
                Member.SetSession(strLoginName);
                if (!string.IsNullOrEmpty(FromUrl))
                {
                    Response.Redirect(FromUrl);
                }
                else
                {
                    Response.Redirect(GlobalVar.WebAppPath + "default.aspx");
                }
            }
            else
            {               
                JSUtil.Alert("用户名或密码错！");
            }

           
        }

        protected void btnGetPwd_Click(object sender, EventArgs e)
        {
            MemberBaseInfo info = Member.GetInfoByLoginName(txtLoginName.Text.Trim());
            if (info == null)
            {
                JSUtil.Alert("用户名不存在");
            }
            else
            {
                SmtpClient client = new SmtpClient("smtp.163.com", 25);
                client.Credentials = new NetworkCredential("cake16875", "cake123456");
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                msg.To.Add(info.Email);

                msg.From = new MailAddress("cake16875@163.com", "999cake", System.Text.Encoding.UTF8);
                /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/
                msg.Subject = "找回密码";//邮件标题 
                msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
                msg.Body = "用户名：" + info.LoginName + " 密码：" + StrUtil.DecryptDES(info.Password,info.LoginName);//邮件内容 
              
                msg.IsBodyHtml = true;//是否是HTML邮件 
               

                
                try
                {
                    client.Send(msg);                   
                    
                    JSUtil.Alert("密码已经您注册的邮箱发送给您了，请注意查收");
                }
                catch (System.Net.Mail.SmtpException ex)
                {
                    ExceptionLogBO.Save(ex);
                    JSUtil.Alert("发送邮件出错,可能您当时注册的邮箱不对"); 
                } 


                
            }
        }
    }
}